package com.haixiaoke.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.common.utils.DateUtils;
import com.haixiaoke.domain.Model;
import com.haixiaoke.domain.Node;
import com.haixiaoke.mapper.BrandMapper;
import com.haixiaoke.domain.Brand;
import com.haixiaoke.mapper.ModelMapper;
import com.haixiaoke.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 品牌Service业务层处理
 *
 * @author xiaolong
 * @date 2023-05-24
 */
@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 查询品牌
     *
     * @param id 品牌主键
     * @return 品牌
     */
    @Override
    public Brand selectBrandById(Long id) {
        return brandMapper.selectBrandById(id);
    }

    /**
     * 查询品牌列表
     *
     * @param brand 品牌
     * @return 品牌
     */
    @Override
    public List<Brand> selectBrandList(Brand brand) {
        return brandMapper.selectBrandList(brand);
    }

    /**
     * 新增品牌
     *
     * @param brand 品牌
     * @return 结果
     */
    @Override
    public int insertBrand(Brand brand) {
        String name = brand.getName();
        Brand brandName =  brandMapper.selectBrandName(name);
        if (ObjectUtil.isNotEmpty(brandName)) {
            throw new ServiceException("品牌已经存在");
        }
        brand.setCreateTime(DateUtils.getNowDate());
        return brandMapper.insertBrand(brand);
    }

    /**
     * 修改品牌
     *
     * @param brand 品牌
     * @return 结果
     */
    @Override
    public int updateBrand(Brand brand) {
        brand.setUpdateTime(DateUtils.getNowDate());
        return brandMapper.updateBrand(brand);
    }

    /**
     * 批量删除品牌
     *
     * @param ids 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deleteBrandByIds(Long[] ids) {
        return brandMapper.deleteBrandByIds(ids);
    }

    /**
     * 删除品牌信息
     *
     * @param id 品牌主键
     * @return 结果
     */
    @Override
    public int deleteBrandById(Long id) {
        return brandMapper.deleteBrandById(id);
    }

    @Override
    public List<Node> cascader() {
        List<Node> brands = brandMapper.selectCascader();
        for (Node brand : brands) {
            Long brandId = brand.getValue();
            List<Node> modelList = modelMapper.selectCascader(brandId);
            brand.setChildren(modelList);
        }
        return brands;
    }

    /**
     * 删除型号和品牌
     * @param modelId 型号id
     * @param brandId 品牌id
     * @return
     */
    @Override
    public int deleteModelAndBrand(Long modelId, Long brandId) {
        List<Model> modelList = modelMapper.selectBrandId(brandId);
        if (modelList.size() == 1) {
            brandMapper.deleteBrandById(brandId);
        }
        return modelMapper.deleteModelById(modelId);
    }


}
